﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using XProject.DataVisit.Model;
using XProject.DataVisit.Model.Temp;
using NHibernate.Transform;

namespace XProject.DataVisit.DAL.Advertisement
{
    public class DAdvertisement : AbstractSupportDal<XProject.DataVisit.Model.Advertisement.Advertisement>
    {
        /// <summary>
        /// 获取自己发布的广告
        /// </summary>
        /// <returns></returns>
        public IList<XProject.DataVisit.Model.Advertisement.Advertisement> GetMyList(UserInfo user,SearchAd c)
        {
            IList<XProject.DataVisit.Model.Advertisement.Advertisement> t = new List<XProject.DataVisit.Model.Advertisement.Advertisement>();
            Template.Execute(delegate(ISession session)
            {
                var tt = session.CreateCriteria<XProject.DataVisit.Model.Advertisement.Advertisement>();
                    if(c.AdName!=null && c.AdName!="")
                        tt.Add(Expression.Like("GName",c.AdName));
                    if (c.SearchType != 9)
                    {
                        tt.Add(Expression.Like("TransfState", c.SearchType));
                    }
                    tt.Add(Expression.Eq("Creater", user.PID));
                    tt.AddOrder(Order.Desc("Lastupdate"));
                    t=tt.List<XProject.DataVisit.Model.Advertisement.Advertisement>();
                return t;
            });
            return t;
        }
        /// <summary>
        /// 获取通过审核
        /// </summary>
        /// <returns></returns>
        public IList<XProject.DataVisit.Model.Advertisement.Advertisement> GetSearchList(SearchAd c)
        {
            IList<XProject.DataVisit.Model.Advertisement.Advertisement> t = new List<XProject.DataVisit.Model.Advertisement.Advertisement>();
            Template.Execute(delegate(ISession session)
            {
                var tt = session.CreateCriteria<XProject.DataVisit.Model.Advertisement.Advertisement>();
                if(c.AdName!=null && c.AdName!="")
                    tt.Add(Expression.Like("GName",c.AdName));
                if (c.SearchType != 9)
                {
                    tt.Add(Expression.Like("TransfState", c.SearchType));
                }
                tt.AddOrder(Order.Desc("Lastupdate"));
                t=tt.List<XProject.DataVisit.Model.Advertisement.Advertisement>();
                return t;
            });
            return t;
        }

        /// <summary>
        /// 获取需要审核的广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList<FlowTemp> GetSHList(Guid id)
        {
            IList<FlowTemp> t = new List<FlowTemp>();
            Template.Execute(delegate(ISession session)
            {
                t = session.GetNamedQuery("getMyFlow")
                    .SetGuid("id", id)
                    .SetResultTransformer(Transformers.AliasToBean<FlowTemp>())
                    .List<FlowTemp>();
                return t;
            });
            return t;
        }
    }
}
